/**
 * 导出excel文件接口
 */
import axios from 'axios'
import { MessageBox } from 'element-ui'
import * as _myTool from '@/common/js/tool.js'
import request from '@/common/js/api'

const tokens = _myTool._getLocalItem('jx_Authorization')
const instance = axios.create({
  timeout: 60000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    Authorization: tokens
  }
})

// 下载请求方法
function doDownLoad(method, url, params, downLoadName, query) {
  // 获取token
  if (method === 'blob') {
    // 导出excel
    // const tokens = _myTool._getLocalItem('jx_Authorization')
    request.request({
      method: 'POST',
      url: url,
      responseType: 'blob',
      // headers: {
      //   'Content-Type': 'application/json;charset=UTF-8',
      //   // 'Content-Type': 'application/vnd.ms-excel;charset=UTF-8',
      //   Authorization: tokens
      // },
      params: query,
      data: params
    })
      .then(function(res) {
        // if (res.status === 200) {
        // 处理返回的文件流
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' }) // new Blob([res])中不加data就会返回下图中[objece objece]内容（少取一层）
        if ('download' in document.createElement('a')) {
          // 支持a标签download的浏览器
          const link = document.createElement('a') // 创建a标签
          link.download = downLoadName + '.xls' // a标签添加属性 //下载文件名称
          // link.download = decodeURIComponent(res.headers["content-disposition"].split(";")[1].split("filename=")[1])
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          document.body.appendChild(link)
          link.click() // 下载
          URL.revokeObjectURL(link.href) // 释放
          document.body.removeChild(link) // 释放
        } else {
          // 其他浏览器
          navigator.msSaveBlob(blob, downLoadName)
        }
        // }
        // else {
        //   MessageBox.alert('操作失败', '提示', {
        //     customClass: 'my_confirm',
        //     confirmButtonText: '确定',
        //     showClose: false
        //   })
        // }
      })
      .catch(function(err) {
        console.log('导出失败', err)
        MessageBox.alert('导出失败', '提示', {
          customClass: 'my_confirm',
          confirmButtonText: '确定',
          showClose: false
        })
      })
  }
}
// 下载添加提示
function downLoadExcel(method, url, params, downLoadName, query) {
  MessageBox.confirm('确认继续下载？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    customClass: 'my_confirm'
  })
    .then(() => {
      doDownLoad(method, url, params, downLoadName, query)
    })
    .catch(() => {})
}

function downDelayPay(orderNumber) {
  // 导出excel
  MessageBox.confirm('确认继续下载？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    customClass: 'my_confirm'
  })
    .then(() => {
      const tokens = _myTool._getLocalItem('jx_Authorization')
      const downLoadName = '赔付订单' + orderNumber
      axios({
        method: 'GET',
        url: '/bds-oms-service/i-bo-deferredPayment/exportExcel/' + orderNumber,
        responseType: 'blob',
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
          Authorization: tokens
        }
      })
        .then(function(res) {
          if (res.status === 200) {
            // 处理返回的文件流
            const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) // new Blob([res])中不加data就会返回下图中[objece objece]内容（少取一层）
            if ('download' in document.createElement('a')) {
              // 支持a标签download的浏览器
              const link = document.createElement('a') // 创建a标签
              link.download = downLoadName + '.xls' // a标签添加属性 //下载文件名称
              // link.download = decodeURIComponent(res.headers["content-disposition"].split(";")[1].split("filename=")[1])
              link.style.display = 'none'
              link.href = URL.createObjectURL(blob)
              document.body.appendChild(link)
              link.click() // 下载
              URL.revokeObjectURL(link.href) // 释放
              document.body.removeChild(link) // 释放
            } else {
              // 其他浏览器
              navigator.msSaveBlob(blob, downLoadName)
            }
          } else {
            MessageBox.alert('操作失败', '提示', {
              confirmButtonText: '确定'
            })
          }
        })
        .catch(function(err) {
          console.log('导出失败', err)
        })
    })
    .catch(() => {})
}

function downLoadVideo(method, url, params, downLoadName) {
  // 获取token
  if (method === 'blob') {
    // 导出excel
    const tokens = _myTool._getLocalItem('jx_Authorization')
    axios({
      method: 'POST',
      url: url,
      responseType: 'blob',
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        // 'Content-Type': 'application/vnd.ms-excel;charset=UTF-8',
        Authorization: tokens
      },
      data: params,
      params: null
    })
      .then(function(res) {
        if (res.status === 200) {
          // 处理返回的文件流
          const blob = new Blob([res.data], { type: 'video/mp4' }) // new Blob([res])中不加data就会返回下图中[objece objece]内容（少取一层）
          if ('download' in document.createElement('a')) {
            // 支持a标签download的浏览器
            const link = document.createElement('a') // 创建a标签
            link.download = downLoadName + '.mp4' // a标签添加属性 //下载文件名称
            // link.download = decodeURIComponent(res.headers["content-disposition"].split(";")[1].split("filename=")[1])
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            document.body.appendChild(link)
            link.click() // 下载
            URL.revokeObjectURL(link.href) // 释放
            document.body.removeChild(link) // 释放
          } else {
            // 其他浏览器
            navigator.msSaveBlob(blob, downLoadName)
          }
        } else {
          MessageBox.alert('操作失败', '提示', {
            customClass: 'my_confirm',
            confirmButtonText: '确定',
            showClose: false
          })
        }
      })
      .catch(function(err) {
        console.log('下载失败', err)
        MessageBox.alert('下载失败', '提示', {
          customClass: 'my_confirm',
          confirmButtonText: '确定',
          showClose: false
        })
      })
  }
}

export { downLoadExcel, downDelayPay, downLoadVideo }
